#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cstdio>
#include <climits>
#include <bitset>
#include <numeric>

using namespace std;
using ui = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
static constexpr int MAX =  5050;


int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n; cin >> n;
    // vector<int> vec1(n), vec2(n);
    // for (int i = 0; i < n; i++) cin >> vec1[i];
    // for (int i = 0; i < n; i++) cin >> vec2[i];
    // vector<vector<int>> dp(n + 1, vector<int>(n + 1));
    // for (int i = 1; i <= n; i++)
    // {
    //     for (int j = 1; j <= n; j++)
    //     {
    //         if (vec1[i - 1] == vec2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
    //         dp[i][j] = max(dp[i][j], max(dp[i][j - 1], dp[i - 1][j]));
    //     }
    // }
    // cout << dp[n][n];
    vector<pii> vp(n + 1);
    for (int i = 1; i <= n; i++) 
    {
        int x; cin >> x;
        vp[x].first = i;
    }
    for (int i = 1; i <= n; i++)
    {
        int x; cin >> x;
        vp[x].second = i;
    }
    sort(vp.begin() + 1, vp.end(), [](pii a, pii b){
        return a.first < b.first;
    });
    int target = 0;
    vector<int> vec(n + 1);
    vec[0] = -1;
    for (int i = 1; i <= n; i++)
    {
        if (vp[i].second > vec[target]) 
        {
            target++;
            vec[target] = vp[i].second;
        }
        else *lower_bound(vec.begin() + 1, vec.begin() + target + 1, vp[i].second) = vp[i].second;
    }
    cout << target;

    return 0;
}














